package com.sanqing.iframe;

import com.sanqing.dao.Dao;
import com.sanqing.main.ComBoxMyModel;
import com.sanqing.main.MyDocument;
import com.sanqing.model.BookInfo;
import com.sanqing.model.BookType;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class BookOrderEditIFrame extends JInternalFrame{
    BookInfo bookInfo;
    JTextField ISBN = new JTextField("请输入13位花号", 13);
    JComboBox bookTypeComBox = null;
    JTextField bookName = new JTextField(20);
    JTextField writeText = new JTextField(20);
    JTextField translatorText = new JTextField(20);
    JTextField publisherText = new JTextField(20);
    JTextField dateText = new JTextField(20);
    JTextField priceText = new JTextField(20);
    JTextField all_countText = new JTextField(20);
    JButton submit;

    public BookOrderEditIFrame() {
        super();
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        setFrameIcon(new ImageIcon("images/icon.png"));
        final BorderLayout borderLayout = new BorderLayout();           //边框布局管理器
        getContentPane().setLayout(borderLayout);           //设置布局
        setIconifiable(true);               //设置窗体可最小化
        setClosable(true);                  //设置窗体可关闭
        setTitle("修改订单");                 //设置窗体标题
        setBounds(100, 100, 700, 350); //设置窗体位置和大小
        final JPanel mainPanel = new JPanel();          //创建中心面板
        mainPanel.setBorder(new EmptyBorder(5, 0, 170, 0));    //设置边框
        final GridLayout gridLayout = new GridLayout(3, 6);  //创建表格布局管理器
        gridLayout.setVgap(2);          //设置组件之间的垂直距离
        gridLayout.setHgap(2);          //设置组件之间的平行距离
        mainPanel.setLayout(gridLayout);
        getContentPane().add(mainPanel);

        bookInfo = new BookInfo();
        final JLabel ISBNLabel = new JLabel("ISBN:");
        ISBNLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        ISBNLabel.setHorizontalAlignment(SwingConstants.CENTER);
        mainPanel.add(ISBNLabel);
        //创建廿号文本框 "
        ISBN.setDocument(new MyDocument(13));                //书号文本框最大输入值为 13 ISBN.setColumns(13);//设蜜文本框长度
        //ISBN.addKeyListener(new ISBNkeyListener());                       //注册监听器 ISBN.addFocusListener(new ISBNFocusListener。);//注册监听器
        ISBN.setFont(new Font("宋体", Font.PLAIN, 25));
        mainPanel.add(ISBN);
        final JLabel bookTypeLabel = new JLabel("类别:");    //创建书籍类别标签
        bookTypeLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        bookTypeLabel.setHorizontalAlignment(SwingConstants.CENTER);  //对齐方式 bookTypeLabeLsetText ("类别：")；	//设置标签文本
        mainPanel.add(bookTypeLabel);    //添加到中心面板

        ArrayList<BookType> list = Dao.selectBookCategory();    //从数据库中取出图书类别
        ComBoxMyModel comBoxMyModel = new ComBoxMyModel();
        comBoxMyModel.setLst(list);
        bookTypeComBox = new JComboBox(comBoxMyModel);
        bookTypeComBox.setFont(new Font("宋体", Font.PLAIN, 25));
        bookTypeComBox.addItemListener(new BookOrderEditIFrame.booktypeComBoxListener());
        mainPanel.add(bookTypeComBox);

        JLabel bookNameLabel = new JLabel("书名:");
        bookNameLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        bookNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
        bookName.setFont(new Font("宋体", Font.PLAIN, 25));
        //bookName.addActionListener();
        mainPanel.add(bookNameLabel);
        mainPanel.add(bookName);

        JLabel writeLabel = new JLabel("作者:");
        writeLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        writeLabel.setHorizontalAlignment(SwingConstants.CENTER);
        writeText.setFont(new Font("宋体", Font.PLAIN, 25));
        //writeText.addActionListener()
        mainPanel.add(writeLabel);
        mainPanel.add(writeText);

        JLabel translatorLabel = new JLabel("译者:");
        translatorLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        translatorLabel.setHorizontalAlignment(SwingConstants.CENTER);
        translatorText.setFont(new Font("宋体", Font.PLAIN, 25));
        mainPanel.add(translatorLabel);
        mainPanel.add(translatorText);

        JLabel publisherLabel = new JLabel("出版社:");
        publisherLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        publisherLabel.setHorizontalAlignment(SwingConstants.CENTER);
        publisherText.setFont(new Font("宋体", Font.PLAIN, 25));
        mainPanel.add(publisherLabel);
        mainPanel.add(publisherText);

        JLabel dateLabel = new JLabel("出版日期:");
        dateLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        dateLabel.setHorizontalAlignment(SwingConstants.CENTER);
        dateText.setFont(new Font("宋体", Font.PLAIN, 25));
        mainPanel.add(dateLabel);
        mainPanel.add(dateText);

        JLabel priceLabel = new JLabel("价格:");
        priceLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        priceLabel.setHorizontalAlignment(SwingConstants.CENTER);
        priceText.setFont(new Font("宋体", Font.PLAIN, 25));
        mainPanel.add(priceLabel);
        mainPanel.add(priceText);

        JLabel all_countLabel = new JLabel("总数:");
        all_countLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        all_countLabel.setHorizontalAlignment(SwingConstants.CENTER);
        all_countText.setFont(new Font("宋体", Font.PLAIN, 25));
        mainPanel.add(all_countLabel);
        mainPanel.add(all_countText);

        JPanel sumPanel = new JPanel();
        sumPanel.setLayout(new FlowLayout());
        submit = new JButton("搜索");
        JButton reset = new JButton("更新");
        submit.setFont(new Font("宋体", Font.PLAIN, 25));
        reset.setFont(new Font("宋体", Font.PLAIN, 25));
        submit.addActionListener(new searchAction());
        reset.addActionListener(new updateBut());
        sumPanel.add(submit);
        sumPanel.add(reset);
        getContentPane().add(sumPanel, BorderLayout.SOUTH);
    }

    private class booktypeComBoxListener implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent e) {
            BookType bookType = (BookType) e.getItem();
            bookInfo.setTypeid(bookType.getId());
            System.out.println(bookType.getId());
        }
    }

    private class searchAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (!ISBN.getText().equals("")) {
                bookInfo = Dao.selectOrder(ISBN.getText());
                if (bookInfo != null) {
                    System.out.println(461919);
                    bookName.setText(bookInfo.getBookname());
                    bookTypeComBox.setSelectedIndex(bookInfo.getTypeid() - 1);
                    writeText.setText(bookInfo.getWriter());
                    translatorText.setText(bookInfo.getTranslator());
                    publisherText.setText(bookInfo.getPublisher());
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
                    String dateText1=sdf.format(bookInfo.getDate());
                    dateText.setText(dateText1);
                    priceText.setText(String.valueOf(bookInfo.getPrice()));
                    all_countText.setText(String.valueOf(bookInfo.getAll_count()));
                }
            }
        }
    }

    private class updateBut implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (!ISBN.getText().equals("")) {
                bookInfo.setISBN(ISBN.getText());
            } else {
                JOptionPane.showMessageDialog(BookOrderEditIFrame.this, "请输入ISBN码");
                return;
            }
            if (bookInfo.getTypeid() == 0) {
                JOptionPane.showMessageDialog(BookOrderEditIFrame.this, "请选择图书类别");
                return;
            }
            if (!bookName.getText().equals("")) {
                bookInfo.setBookname(bookName.getText());
            } else {
                JOptionPane.showMessageDialog(BookOrderEditIFrame.this, "请输入书名");
                return;
            }
            if (!writeText.getText().equals("")) {
                bookInfo.setWriter(writeText.getText());
            } else {
                JOptionPane.showMessageDialog(BookOrderEditIFrame.this, "请输作者");
                return;
            }
            if (!translatorText.getText().equals("")) {
                bookInfo.setTranslator(translatorText.getText());
            } else {
                JOptionPane.showMessageDialog(BookOrderEditIFrame.this, "请输入译者");
                return;
            }
            if (!publisherText.getText().equals("")) {
                bookInfo.setPublisher(publisherText.getText());
            } else {
                JOptionPane.showMessageDialog(BookOrderEditIFrame.this, "请输入译者");
                return;
            }
            if (!dateText.getText().equals("")) {
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
                try {
                    Date textDate=sdf.parse(dateText.getText());
                    bookInfo.setDate(textDate);
                } catch (ParseException parseException) {
                    parseException.printStackTrace();
                }

            } else {
                JOptionPane.showMessageDialog(BookOrderEditIFrame.this, "请输入出版日期");
                return;
            }
            if (!priceText.getText().equals("")) {
                bookInfo.setPrice(Double.parseDouble(priceText.getText()));
            } else {
                JOptionPane.showMessageDialog(BookOrderEditIFrame.this, "请输入价格");
                return;
            }
            if (!all_countText.getText().equals("")) {
                bookInfo.setAll_count(Integer.parseInt(all_countText.getText()));
            } else {
                JOptionPane.showMessageDialog(BookOrderEditIFrame.this, "请输入总数");
                return;
            }
            System.out.println(bookInfo.toString());


            if (Dao.EditOrder(bookInfo)) {
                JOptionPane.showMessageDialog(BookOrderEditIFrame.this, "更新成功！");

            } else {
                JOptionPane.showMessageDialog(BookOrderEditIFrame.this, "更新失败");
            }
            submit.doClick();
        }
    }
}
